package edu.szu.huhanpeng.doodlingFight.controller.dataController.gameStart 
{
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessBoardData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessFriend;
	import edu.szu.huhanpeng.doodlingFight.interfaces.ISquareData;
	import edu.szu.huhanpeng.doodlingFight.model.ChessBoardProxy;
	import edu.szu.huhanpeng.doodlingFight.model.ChessGroupProxy;
	import edu.szu.huhanpeng.doodlingFight.model.data.ChessData;
	import edu.szu.huhanpeng.doodlingFight.view.components.ChessComponent;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * 找出已经排列成星的组合
	 * @author Huhanpeng
	 */
	public class DestroyDoneDc extends SimpleCommand implements ICommand, INotifier 
	{
		
		override public function execute(note:INotification):void 
		{
			var chessBoardProxy:ChessBoardProxy = facade.retrieveProxy(ChessBoardProxy.NAME) as ChessBoardProxy;
			var allSquare:Vector.<ISquareData> = chessBoardProxy.allSquare;
			
			var typeVecSrc:Vector.<int> = new Vector.<int>();
			var typeVec:Vector.<int>;
			for (var j:int = 1; j <= GlobalConst.setting.CHESS_TYPE ; j++) 
			{
				typeVecSrc.push(j)
			}
			
			var square:ISquareData;
			var chess:IChessData;
			var friend:Vector.<IChessData>;
			for (var i:int = 0; i < allSquare.length; i++) 
			{
				square = allSquare[i]
				chess = square.chessData;
				friend = chess.friend;
				
				if (friend[ChessData.FOUR] != null && friend[ChessData.SIX] != null) 
				{
					typeVec = typeVecSrc.concat();
					typeVec.splice(typeVec.indexOf(chess.type), 1);
					
					//上下方若是同类则在各自的friend属性删去对方，不是同类则在待选类型中除去该类型。
					if (square.upSquare != null) 
					{
						if (square.upSquare.chessData.type != chess.type) 
						{
							typeVec.splice(typeVec.indexOf(square.upSquare.chessData.type), 1);
						}
						else 
						{
							chess.friend[ChessData.TWO].friend[ChessData.EIGHT] = null;
							chess.friend[ChessData.TWO] = null;
						}
					}
					
					if (square.downSquare != null) 
					{
						if (square.downSquare.chessData.type != chess.type) 
						{
							typeVec.splice(typeVec.indexOf(square.downSquare.chessData.type), 1);
						}
						else 
						{
							chess.friend[ChessData.EIGHT].friend[ChessData.TWO] = null;
							chess.friend[ChessData.EIGHT] = null;
						}
					}
					
					//删除左右的同类属性
					friend[ChessData.FOUR].friend[ChessData.SIX] = friend[ChessData.SIX].friend[ChessData.FOUR] = null;
					friend[ChessData.FOUR] = friend[ChessData.SIX] = null;
					
					
					if (typeVec.length == 0) 
					{
						throw(new Error("棋子种类小于等于3，destroy失败"))
					}
					
					//chess.type = typeVec[Math.floor(Math.random() * typeVec.length)];
					chess.type = typeVec[0];
					var chessMed:IMediator = facade.retrieveMediator(chess.name);
					(chessMed.getViewComponent() as ChessComponent).type　 = chess.type;
					
					//检查设置1，3，7，9号方位的同类属性
					set1379Friend(square,chess,friend);
				}
				//上下方向的若可消除也已在上方处理过。
				else if (friend[ChessData.TWO] != null && friend[ChessData.EIGHT] != null) 
				{
					typeVec = typeVecSrc.concat();
					typeVec.splice(typeVec.indexOf(chess.type), 1);
					
					
					if (square.leftSquare != null) 
					{
						if (square.leftSquare.chessData.type != chess.type) 
						{
							typeVec.splice(typeVec.indexOf(square.leftSquare.chessData.type), 1);
						}
						else 
						{
							chess.friend[ChessData.FOUR].friend[ChessData.SIX] = null;
							chess.friend[ChessData.FOUR] = null;
						}
					}
					
					if (square.rightSquare != null) 
					{
						if (square.rightSquare.chessData.type != chess.type) 
						{
							typeVec.splice(typeVec.indexOf(square.rightSquare.chessData.type), 1);
						}
						else 
						{
							chess.friend[ChessData.SIX].friend[ChessData.FOUR] = null;
							chess.friend[ChessData.SIX] = null;
						}
					}
					
					friend[ChessData.TWO].friend[ChessData.EIGHT] = friend[ChessData.EIGHT].friend[ChessData.TWO] = null;
					friend[ChessData.TWO] = friend[ChessData.EIGHT] = null;
					if (typeVec.length == 0) 
					{
						throw(new Error("棋子种类小于等于3，destroy失败"))
					}

					
					//chess.type = typeVec[Math.floor(Math.random() * typeVec.length)];
					chess.type = typeVec[0];
					(facade.retrieveMediator(chess.name).getViewComponent() as ChessComponent).type　 = chess.type;
					set1379Friend(square,chess,friend);
				}
				
			}
		}
		
		//检查设置1，3，7，9号方位的同类属性
		private function set1379Friend(square:ISquareData, chess:IChessData, friend:Vector.<IChessData>):void 
		{
			if (square.LUSquare != null) 
			{
				if (square.LUSquare.chessData.type == chess.type) 
				{
					chess.friend[ChessData.ONE] = square.LUSquare.chessData;
					chess.friend[ChessData.ONE].friend[ChessData.NINE] = chess;
				}
				else
				{
					chess.friend[ChessData.ONE] = null;
					square.LUSquare.chessData.friend[ChessData.NINE] = null;
				}
			}
			
			if (square.RUSquare != null) 
			{
				if (square.RUSquare.chessData.type == chess.type) 
				{
					chess.friend[ChessData.THREE] = square.RUSquare.chessData;
					chess.friend[ChessData.THREE].friend[ChessData.SEVEN] = chess;
				}
				else
				{
					chess.friend[ChessData.THREE] = null;
					square.RUSquare.chessData.friend[ChessData.SEVEN] = null;
				}
			}
			
			if (square.LDSquare != null) 
			{
				if (square.LDSquare.chessData.type == chess.type) 
				{
					chess.friend[ChessData.SEVEN] = square.LDSquare.chessData;
					chess.friend[ChessData.SEVEN].friend[ChessData.THREE] = chess;
				}
				else
				{
					chess.friend[ChessData.SEVEN] = null;
					square.LDSquare.chessData.friend[ChessData.THREE] = null;
				}
			}
			
			if (square.RDSquare != null) 
			{
				if (square.RDSquare.chessData.type == chess.type) 
				{
					chess.friend[ChessData.NINE] = square.RDSquare.chessData;
					chess.friend[ChessData.NINE].friend[ChessData.ONE] = chess;
				}
				else
				{
					chess.friend[ChessData.NINE] = null;
					square.RDSquare.chessData.friend[ChessData.ONE] = null;
				}
			}
		}
	}

}